package com.sisgh.jefdv1.core.entity.field;

import com.sisgh.jefdv1.lang.FormatStringBuilder;
import com.sisgh.jefdv1.util.StringUtils;
import lombok.Data;

/**
 * 类字段
 */
@Data
public class Field {

    private static final String PARENTHESES_START = "(";
    private static final String PARENTHESES_END = ")";

    private static final String BRACE_START = "{";
    private static final String BRACE_END = "}";

    private static final String END = ";";

    // name
    private String name;

    private String type;

    Field() {}

    Field(String name, String type) {
        this.name = name;
        this.type = type;
    }

    public String toPrivate() {
        return "private " + type + " " + name;
    }

    public String toGetter() {
        FormatStringBuilder fsb = new FormatStringBuilder(END);
        return fsb.tab()
                .appendWithSpace("public")
                .appendWithSpace("void")
                .append(StringUtils.buildCamel("get", name))
                .append(PARENTHESES_START)
                .append(PARENTHESES_END)
                .appendWithNewline(BRACE_START)
                .tab(2)
                .appendWithSpace("return")
                .append("this.")
                .append(name)
                .endWithNewline()
                .appendWithPrefixTab(BRACE_END)
                .toString();
    }

    public String toSetter() {
        FormatStringBuilder fsb = new FormatStringBuilder(END);
        return fsb.tab()
                .appendWithSpace("public")
                .appendWithSpace(type)
                .append(StringUtils.buildCamel("set", name))
                .append(PARENTHESES_START)
                .appendWithSpace(type)
                .append(name)
                .append(PARENTHESES_END)
                .appendWithNewline(BRACE_START)
                .tab(2)
                .append("this.")
                .appendWithSpace(name)
                .appendWithSpace("=")
                .append(name)
                .endWithNewline()
                .appendWithPrefixTab(BRACE_END)
                .toString();
    }

    @Override
    public String toString() {
        return "Field [name=" + name + ", type=" + type + "]";
    }
}
